﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using TradingAppDal;
using TradingApp.Helper;

namespace TradingApp.Admin
{
    public partial class frmConnectionString : Form
    {
        public frmConnectionString()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateConnectionString())
                {
                    TradingAppSettings.DBConnectionStr = tbConnStr.Text.Trim();

                    XmlHelper.SetTagValue(TradingAppSettings.CONN_ATTRIBUTE_STR,TradingAppSettings.DBConnectionStr);
                    MessageBox.Show("Connection String successfully changed.", "Connection String", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }

        private bool ValidateConnectionString()
        {
            if (String.IsNullOrEmpty(tbConnStr.Text.Trim()))
            {
                MessageBox.Show("Connection String cannot be empty.", "Connection String", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }


            return true;
        }

        private void frmPoolInterval_Load(object sender, EventArgs e)
        {
            tbConnStr.Text = TradingAppSettings.DBConnectionStr;
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            try
            {
                if (AdminDal.TestConnectionString(tbConnStr.Text.Trim()))
                {
                    MessageBox.Show("Connected.", "Connection String", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Can't Connect.", "Connection String", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (System.Exception ex)
            {
                UIHelper.HandleApplicationException(ex);
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
